package com.hqu.environment.controller.admin;

import com.hqu.environment.annotation.IgnoreAuth;
import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.UserDao;
import com.hqu.environment.dto.UserDTO;
import com.hqu.environment.enums.BooleanEnum;
import com.hqu.environment.utils.ExceptionCode;
import com.hqu.environment.utils.JwtHelper;
import com.hqu.environment.utils.Result;
import com.hqu.environment.utils.Util;
import com.hqu.environment.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 用户Controller
 * @Date: 2019/04/19 14:47
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    UserDao userDao;

    //登录
    @IgnoreAuth
    @RequestMapping("/login")
    @ResponseBody
    public Result login(UserVO userVO) {
        if(userDao.selectCountByEmpno(userVO.getLoginId())==0&&userDao.selectCountByPhone(userVO.getLoginId())==0&&userDao.selectCountByEmail(userVO.getLoginId())==0)
            return Result.fail(ExceptionCode.USER_NOT_FOUND);

        UserDTO userDTO=userDao.selectPasswordByLoginId(userVO.getLoginId());
        //判断管理员是否同意注册
        if(userDTO.getIsAdmin()== BooleanEnum.NO.getIndex())
            return Result.fail(ExceptionCode.USER_NOT_FOUND);

        //判断密码是否正确
        if(!userDTO.getPassword().equals(userVO.getPassword()))
            return Result.fail(ExceptionCode.PASSWORD_ERROR);

        //生成token
        String token = JwtHelper.genToken(userDTO.getId());
        return Result.success().add("token",token);
    }


    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(@LoginUser Integer userId){
        UserDTO user = userDao.selectByPrimaryKey(userId);
        return Result.success().add("user",user);
    }

    @RequestMapping("/getUserInfoById")
    @ResponseBody
    public Result getUserInfoById(Integer id){
        UserDTO user = userDao.selectByPrimaryKey(id);
        return Result.success().add("user",user);
    }

    @RequestMapping("/getApplyUserList")
    @ResponseBody
    public Result getApplyUserList(UserDTO userDTO){
        userDTO.setIsAgree(BooleanEnum.NO.getIndex());
        List<UserDTO> u = userDao.selectList(userDTO);
        return Result.success().add("userList",u);
    }


    @RequestMapping("/getUserList")
    @ResponseBody
    public Result getUserList(UserDTO userDTO){
        userDTO.setIsAgree(BooleanEnum.YES.getIndex());
        List<UserDTO> u = userDao.selectList(userDTO);
        return Result.success().add("userList",u);
    }

    @RequestMapping("/agreeApply")
    @ResponseBody
    public Result agreeApply(UserDTO userDTO){
        userDTO.setIsAgree(BooleanEnum.YES.getIndex());
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }

    @RequestMapping("/refuseApply")
    @ResponseBody
    public Result refuseApply(Integer id){
        userDao.deleteByPrimaryKey(id);
        return Result.success();
    }

    @RequestMapping("/agreeAdmin")
    @ResponseBody
    public Result agreeAdmin(UserDTO userDTO){
        userDTO.setIsAdmin(BooleanEnum.YES.getIndex());
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }

    @RequestMapping("/freeze")
    @ResponseBody
    public Result freeze(UserDTO userDTO){
        userDTO.setIsFreeze(BooleanEnum.YES.getIndex());
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }

    @RequestMapping("/unFreeze")
    @ResponseBody
    public Result unFreeze(UserDTO userDTO){
        userDTO.setIsFreeze(BooleanEnum.NO.getIndex());
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }

    @RequestMapping("/updateAuthority")
    @ResponseBody
    public Result updateAuthority(UserDTO userDTO){
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }


    @RequestMapping("/alterUserInfo")
    @ResponseBody
    public Result alterUserInfo(@LoginUser Integer userId,UserVO userVO){
        userVO.setId(userId);
        UserDTO userDTO =new UserDTO();
        BeanUtils.copyProperties(userVO, userDTO);
        userDao.updateByPrimaryKeySelective(userDTO);
        return Result.success();
    }

}
